package com.ae.putingotdown;

import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.tags.ITag;
import net.minecraft.tags.ItemTags;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.registries.ForgeRegistries;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

public class Config {
    public static ForgeConfigSpec CONFIG;
    public static ForgeConfigSpec CLIENT_CONFIG;
    public static ForgeConfigSpec.IntValue MAX_COUNT;
    public static ForgeConfigSpec.ConfigValue<List<? extends String>> ITEMS;
    public static ForgeConfigSpec.ConfigValue<List<? extends String>> TAGS;
    public static ForgeConfigSpec.BooleanValue CAN_ALL_PUT;
    public static ForgeConfigSpec.IntValue DISTANCE;
    public static HashMap<Item, Integer> ITEMS_MAP = new HashMap<>();
    public static HashMap<ITag<Item>, Integer> TAGS_MAP = new HashMap<>();

    static {
        ForgeConfigSpec.Builder builder = new ForgeConfigSpec.Builder();
        builder.push("Common");
        MAX_COUNT = builder.comment("The max ingot count every pile.").defineInRange("MaxCount", 64, 1, Integer.MAX_VALUE);
        ArrayList<String> list = new ArrayList<>(Arrays.asList("creat:andesite_alloy", "878787", "create:polished_rose_quartz",
                "15685206", "astralsorcery:starmetal_ingot", "4457", "extrabotany:aerialite", "491169"));
        ITEMS = builder.comment("Config what ingot can be placed.(Defined by item id, priority over tag.)").defineList("Item", list, o -> o instanceof String);
        ArrayList<String> list1 = new ArrayList<>(Arrays.asList("forge:ingots/iron", "16777215", "forge:ingots/gold",
                "16768512", "forge:ingots/netherite", "4473153", "forge:ingots/copper", "12542976", "forge:ingots/tin",
                "10470592", "forge:ingots/lead", "5660024", "forge:ingots/silver", "12372687", "forge:ingots/nickel",
                "12824692", "forge:ingots/bronze", "13994795", "forge:ingots/electrum", "15128436", "forge:ingots/invar",
                "10596262", "forge:ingots/constantan", "14128461", "forge:ingots/signalum", "15480326", "forge:ingots/lumium",
                "15850096", "forge:ingots/enderium", "1409424", "forge:ingots/osmium", "10202052", "forge:ingots/refined_obsidian",
                "6441607", "forge:ingots/steel", "6710886", "forge:ingots/refined_glowstone", "16769127", "forge:ingots/uranium",
                "10809765", "forge:ingots/ardite", "9711635", "forge:ingots/cobalt", "2324189", "forge:ingots/knight_slime",
                "12807921", "forge:ingots/manyullyn", "7422380", "forge:ingots/pig_iron", "8353143", "forge:ingots/aluminum",
                "13560049", "forge:ingots/zinc", "12107145", "forge:ingots/brass", "12964214", "forge:ingots/elementium",
                "15817902", "forge:ingots/manasteel", "3377663", "forge:ingots/terrasteel", "5503232", "forge:ingots/compressed_iron",
                "6513507", "forge:ingots/hellforged", "10148301"));
        TAGS = builder.comment("Config what ingot can be placed.(Defined by item tag.)").defineList("Tags", list1, o -> o instanceof String);
        CAN_ALL_PUT = builder.comment("Ture to allow any items can putting down.").define("CanAllPut", true);
        builder.pop();
        CONFIG = builder.build();

        ForgeConfigSpec.Builder cBuilder = new ForgeConfigSpec.Builder();
        cBuilder.push("Client");
        DISTANCE = cBuilder.comment("Render distance.").defineInRange("Distance", 160, 1, 160);
        cBuilder.pop();
        CLIENT_CONFIG = cBuilder.build();
    }

    public static int test(ItemStack stack) {
        Item item = stack.getItem();

        if (ITEMS_MAP.containsKey(item)) {
            return ITEMS_MAP.get(item);
        }

        for (ITag<Item> tag : TAGS_MAP.keySet()) {
            if (tag.contains(item)) {
                return TAGS_MAP.get(tag);
            }
        }

        return -1;
    }

    public static void arrayToMap() {
        if (ITEMS_MAP.isEmpty() && !ITEMS.get().isEmpty()) {
            List<? extends String> itemList = ITEMS.get();
            for (int i = 0, getSize = itemList.size(); i < getSize; i = i + 2) {
                String id = itemList.get(i);
                Item item = ForgeRegistries.ITEMS.getValue(new ResourceLocation(id));

                if (item == null) {
                    continue;
                }

                ITEMS_MAP.put(item, Integer.valueOf(itemList.get(i + 1)));
            }
        }

        if (TAGS_MAP.isEmpty() && !TAGS.get().isEmpty()) {
            List<? extends String> tagList = TAGS.get();
            for (int i = 0, tagListSize = tagList.size(); i < tagListSize; i = i + 2) {
                String tag = tagList.get(i);
                ITag<Item> namedTag = ItemTags.createOptional(new ResourceLocation(tag));

                try{
                    namedTag.getValues();
                    TAGS_MAP.put(namedTag, Integer.valueOf(tagList.get(i + 1)));
                }catch (IllegalStateException ignored){

                }
            }
        }
    }
}
